package com.briup.day29;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author ThinkBook
 */
public class ReversalServer {
    public static void main(String[] args) throws IOException {
        //1.搭建服务器
        ServerSocket serverSocket = new ServerSocket(8989);
        System.out.println("服务器启动成功，ServerSocket："+serverSocket);
        //2.接收客户端连接
        Socket socket = serverSocket.accept();
        System.out.println("客户端连接成功，socket"+ socket);

        //3.获取IO流并增强
        // 获取输入流（获取一行）
        BufferedReader br = new BufferedReader(
                new InputStreamReader(socket.getInputStream())
        );
        // 获取输出流
        PrintStream ps = new PrintStream(socket.getOutputStream());
        String line ;
        //创建一个反转对象
        StringBuffer sb = new StringBuffer();
        while (true){
             line = br.readLine();
            System.out.println("read: " + line);
            sb.append(line);
            sb.reverse();
            if ("quit".equals(line)){
                 break;
             }

             ps.println(sb);
            System.out.println("服务器 成功发送 反转信息...");
            sb.delete(0,sb.length());
        }
        ps.close();
        br.close();
        socket.close();
        serverSocket.close();
    }
}
